package com.uin.saas.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: wyy-sss
 * @date: 2019-09-04 17:10
 **/
//@Slf4j
//@Aspect
//@Component
//@EnableAspectJAutoProxy(proxyTargetClass = true)
//@Order(-99)
public class DynamicDataSourceAspectAdvice {

//    @Around("execution(* com.uin.saas.*.controller.*.*(..))")
//    public Object doAround(ProceedingJoinPoint jp) throws Throwable {
//        ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = sra.getRequest();
//        HttpServletResponse response = sra.getResponse();
//        String tenantKey = request.getHeader("tenant");
//        // 前端必须传入tenant header, 否则返回400
//        if (!StringUtils.hasText(tenantKey)) {
//           // WebUtils.toHttp(response).sendError(HttpServletResponse.SC_BAD_REQUEST);
//            return null;
//        }
//        log.info("当前租户key:{}", tenantKey);
//        DataSourceContextHolder.setDBType(tenantKey);
//        Object result = jp.proceed();
//        DataSourceContextHolder.clearDBType();
//        return result;
//    }


}
